package com.tcwlkj.uuService.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.XieYiModel;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import java.net.URLDecoder;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/10/8 0008.
 */

public class UserXieYi extends BaseActivity {
    @Bind(R.id.web_xieyi)
    WebView webXieyi;
    private String mType;
    @Bind(R.id.iv_me)
    ImageView iv_me;
    @Bind(R.id.tv_me)
    TextView tv_me;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_xie_yi);
        ButterKnife.bind(this);
        mType = getIntent().getStringExtra("XIEYI_TYPE");
        iv_me.setVisibility(View.GONE);
        tv_me.setVisibility(View.GONE);

        if (mType.equals("TC-ZCXY-PN002")) {
            changeTitle("服务条款");//注册协议
            getXieYis("TC-ZCXY-PN002");
            //getXieYi("1", true);
        } else if (mType.equals("2")) {
            changeTitle("提现规则");
            //getXieYi("2", true);
        } else if (mType.equals("3")) {
            changeTitle("保险说明");
            //getXieYi("3", true);
        } else if (mType.equals("4")) {
            changeTitle("同城课堂");
            //getXieYi("8", true);
        } else if (mType.equals("5")) {
            changeTitle("关于我们");//系统设置中的协议
            getXieYis("TC-GYWM-PN001");
            tv_me.setText("同城跑男:v"+Tools.getVersion(baseContext));
            tv_me.setVisibility(View.VISIBLE);
            iv_me.setVisibility(View.VISIBLE);
            //getXieYi("6", true);
        }else if(mType.equals("15")){
            changeTitle("接单流程");
            getXieYis("TC-JDLC-PN001");
            //getXieYi("15", true);
        }else if(mType.equals("16")){
            changeTitle("常见问题");
            getXieYis("TC-CJWT-PN004");
            //getXieYi("16", true);
        }else if(mType.equals("17")){
            changeTitle("服务标准");
            getXieYis("TC-FWBZ-PN002");
            //getXieYi("17", true);
        }else if(mType.equals("18")){
            changeTitle("管理规定");
            getXieYis("TC-GLGD-PN003");
            //getXieYi("18", true);
        }else if(mType.equals("19")){
            changeTitle("结算须知");
            getXieYis("TC-JSXZ-PN005");
            //getXieYi("19", true);
        }else if(mType.equals("20")){
            changeTitle("赔付规则");
            getXieYis("TC-PFGZ-PN006");
            //getXieYi("20", true);
        }else if(mType.equals("21")){
            changeTitle("保险规则");
            //getXieYi("21", true);
        }
    }

//    public void getXieYi(String type, boolean isLoading) {
//        Call<XieYiModel> httpResultCall = RetrofitUtils.getRequestService().Get_AppText("Get_AppText", type);
//        httpResultCall.enqueue(new Callback<XieYiModel>() {
//            @Override
//            public void onResponse(Call<XieYiModel> call, Response<XieYiModel> response) {
//                XieYiModel xieYiModel=response.body();
//                webXieyi.setWebViewClient(new WebViewClient());
//                webXieyi.loadDataWithBaseURL(null, xieYiModel.getData().getText(), "text/html", "utf-8", null);
//            }
//
//            @Override
//            public void onFailure(Call<XieYiModel> call, Throwable t) {
//
//            }
//        });
//    }

    public void getXieYis(String type) {
        Call<CommonResponse<String>> httpResultCall = RetrofitUtils.getRequest(this,false).get_xieyi(type);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<String>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<String> commonResponse= (CommonResponse<String>) t;
                //String str= (String) t;
                webXieyi.setWebViewClient(new WebViewClient());
                webXieyi.loadDataWithBaseURL(null, URLDecoder.decode(commonResponse.getData()), "text/html", "utf-8", null);
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    getXieYis(type);
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
